<?php
/**
 * @file statisticcategory.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-9-16 下午5:03:11 $
 * @brief  $
 */
namespace haibao\app\business;

class StatisticCategory extends \haibao\app\business\Base {

	/**
	 * @var $_instance
	 */
	private static $_instance;

	/**
	 * instance()
	 */
	private static function instance(){
		if(!self::$_instance){
			self::$_instance = new \haibao\app\data\StatisticCategory();
		}
		return self::$_instance;
	}

	/**
	 * 获取行为列表
	 */
	public static function getAppActionCountList(){
		return self::instance()->getAppActionCountList();
	}
	
	/**
	 * 添加行为名称
	 */
	public static function addAction($model){
		self::checkOperation(self::FUNCTION_CODE_APP_STATISTIC_ACTION);
		self::checkActionForm($model);
		self::instance()->addAction($model);
		self::saveOperateHistory(self::FUNCTION_CODE_APP_STATISTIC_ACTION, $model->toArray(true,true));
	}
	
	public static function getAppAction($action,$type){
		return self::instance()->getAppAction($action,$type);
	}

	/**
	 * 验证
	 */
	private static function checkActionForm($model){
		if(!isset($model->Name)){
			throw new BusinessException('行为名称不能为空');
		}
		
		if(!isset($model->Code)){
			throw new BusinessException('行为编码不能为空');
		}
		$result =  self::instance()->getAppActionCountList();
		
		if(isset($model->Name)){
			foreach($result as $key=>$v){
				if($v['Name'] == $model->Name){
					throw new BusinessException('行为名称不能重复');
				}
			}
		}
		if(isset($model->Code)){
			foreach($result as $key=>$v){
				if($v['Code'] == $model->Code){
					throw new BusinessException('行为编码不能重复');
				}
			}
				
		}
		
	}
	
}